/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package GM.impl;

import GM.GMPackage;
import GM.Goal;
import GM.RefType;
import GM.RefinementLink;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.impl.EObjectImpl;

import org.eclipse.emf.ecore.util.EObjectWithInverseResolvingEList;
import org.eclipse.emf.ecore.util.InternalEList;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Refinement Link</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link GM.impl.RefinementLinkImpl#getId <em>Id</em>}</li>
 *   <li>{@link GM.impl.RefinementLinkImpl#getType <em>Type</em>}</li>
 *   <li>{@link GM.impl.RefinementLinkImpl#getTarget <em>Target</em>}</li>
 *   <li>{@link GM.impl.RefinementLinkImpl#getMyFather <em>My Father</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class RefinementLinkImpl extends EObjectImpl implements RefinementLink {
	/**
	 * The default value of the '{@link #getId() <em>Id</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getId()
	 * @generated
	 * @ordered
	 */
	protected static final int ID_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getId() <em>Id</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getId()
	 * @generated
	 * @ordered
	 */
	protected int id = ID_EDEFAULT;

	/**
	 * The default value of the '{@link #getType() <em>Type</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getType()
	 * @generated
	 * @ordered
	 */
	protected static final RefType TYPE_EDEFAULT = RefType.AND;

	/**
	 * The cached value of the '{@link #getType() <em>Type</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getType()
	 * @generated
	 * @ordered
	 */
	protected RefType type = TYPE_EDEFAULT;

	/**
	 * The cached value of the '{@link #getTarget() <em>Target</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTarget()
	 * @generated
	 * @ordered
	 */
	protected EList<Goal> target;

	/**
	 * The cached value of the '{@link #getMyFather() <em>My Father</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getMyFather()
	 * @generated
	 * @ordered
	 */
	protected Goal myFather;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected RefinementLinkImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return GMPackage.Literals.REFINEMENT_LINK;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public int getId() {
		return id;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setId(int newId) {
		int oldId = id;
		id = newId;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, GMPackage.REFINEMENT_LINK__ID, oldId, id));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public RefType getType() {
		return type;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setType(RefType newType) {
		RefType oldType = type;
		type = newType == null ? TYPE_EDEFAULT : newType;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, GMPackage.REFINEMENT_LINK__TYPE, oldType, type));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<Goal> getTarget() {
		if (target == null) {
			target = new EObjectWithInverseResolvingEList<Goal>(Goal.class, this, GMPackage.REFINEMENT_LINK__TARGET, GMPackage.GOAL__SON_OF);
		}
		return target;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Goal getMyFather() {
		if (myFather != null && myFather.eIsProxy()) {
			InternalEObject oldMyFather = (InternalEObject)myFather;
			myFather = (Goal)eResolveProxy(oldMyFather);
			if (myFather != oldMyFather) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, GMPackage.REFINEMENT_LINK__MY_FATHER, oldMyFather, myFather));
			}
		}
		return myFather;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Goal basicGetMyFather() {
		return myFather;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetMyFather(Goal newMyFather, NotificationChain msgs) {
		Goal oldMyFather = myFather;
		myFather = newMyFather;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, GMPackage.REFINEMENT_LINK__MY_FATHER, oldMyFather, newMyFather);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setMyFather(Goal newMyFather) {
		if (newMyFather != myFather) {
			NotificationChain msgs = null;
			if (myFather != null)
				msgs = ((InternalEObject)myFather).eInverseRemove(this, GMPackage.GOAL__REF, Goal.class, msgs);
			if (newMyFather != null)
				msgs = ((InternalEObject)newMyFather).eInverseAdd(this, GMPackage.GOAL__REF, Goal.class, msgs);
			msgs = basicSetMyFather(newMyFather, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, GMPackage.REFINEMENT_LINK__MY_FATHER, newMyFather, newMyFather));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case GMPackage.REFINEMENT_LINK__TARGET:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getTarget()).basicAdd(otherEnd, msgs);
			case GMPackage.REFINEMENT_LINK__MY_FATHER:
				if (myFather != null)
					msgs = ((InternalEObject)myFather).eInverseRemove(this, GMPackage.GOAL__REF, Goal.class, msgs);
				return basicSetMyFather((Goal)otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case GMPackage.REFINEMENT_LINK__TARGET:
				return ((InternalEList<?>)getTarget()).basicRemove(otherEnd, msgs);
			case GMPackage.REFINEMENT_LINK__MY_FATHER:
				return basicSetMyFather(null, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case GMPackage.REFINEMENT_LINK__ID:
				return getId();
			case GMPackage.REFINEMENT_LINK__TYPE:
				return getType();
			case GMPackage.REFINEMENT_LINK__TARGET:
				return getTarget();
			case GMPackage.REFINEMENT_LINK__MY_FATHER:
				if (resolve) return getMyFather();
				return basicGetMyFather();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case GMPackage.REFINEMENT_LINK__ID:
				setId((Integer)newValue);
				return;
			case GMPackage.REFINEMENT_LINK__TYPE:
				setType((RefType)newValue);
				return;
			case GMPackage.REFINEMENT_LINK__TARGET:
				getTarget().clear();
				getTarget().addAll((Collection<? extends Goal>)newValue);
				return;
			case GMPackage.REFINEMENT_LINK__MY_FATHER:
				setMyFather((Goal)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case GMPackage.REFINEMENT_LINK__ID:
				setId(ID_EDEFAULT);
				return;
			case GMPackage.REFINEMENT_LINK__TYPE:
				setType(TYPE_EDEFAULT);
				return;
			case GMPackage.REFINEMENT_LINK__TARGET:
				getTarget().clear();
				return;
			case GMPackage.REFINEMENT_LINK__MY_FATHER:
				setMyFather((Goal)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case GMPackage.REFINEMENT_LINK__ID:
				return id != ID_EDEFAULT;
			case GMPackage.REFINEMENT_LINK__TYPE:
				return type != TYPE_EDEFAULT;
			case GMPackage.REFINEMENT_LINK__TARGET:
				return target != null && !target.isEmpty();
			case GMPackage.REFINEMENT_LINK__MY_FATHER:
				return myFather != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (id: ");
		result.append(id);
		result.append(", type: ");
		result.append(type);
		result.append(')');
		return result.toString();
	}

} //RefinementLinkImpl
